package com.ahua.mallchat.common.controller;

import com.ahua.mallchat.common.domain.RequestHolder;
import com.ahua.mallchat.common.domain.dto.*;
import com.ahua.mallchat.common.domain.enums.user.RoleEnum;
import com.ahua.mallchat.common.domain.vo.ApiResult;
import com.ahua.mallchat.common.domain.vo.user.BadgeResp;
import com.ahua.mallchat.common.domain.vo.user.ItemInfoVO;
import com.ahua.mallchat.common.domain.vo.user.SummeryInfoVO;
import com.ahua.mallchat.common.domain.vo.user.UserInfoResp;
import com.ahua.mallchat.common.service.RoleService;
import com.ahua.mallchat.common.service.UserService;
import com.ahua.mallchat.common.utils.AssertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/capi/user")
@Api(tags = "用户管理相关接口")
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;

    /**
     * 获取用户详情
     */
    @GetMapping
    public ApiResult<UserInfoResp> getUserInfo() {
        return ApiResult.success(userService.getUserInfo(RequestHolder.get().getUid()));
    }

    /**
     * 修改用户名
     */
    @PutMapping("name")
    public ApiResult<Void> modifyName(@Validated @RequestBody ModifyNameReq req) {
        userService.modifyName(RequestHolder.get().getUid(), req);
        return ApiResult.success();
    }

    /**
     * 获取勋章列表
     */
    @GetMapping("badges")
    public ApiResult<List<BadgeResp>> badges() {
        List<BadgeResp> badgeRespList = userService.badges(RequestHolder.get().getUid());
        return ApiResult.success(badgeRespList);
    }

    /**
     * 佩戴勋章
     */
    @PutMapping("badge")
    public ApiResult<Void> wearingBadge(@Validated @RequestBody WearingBadgeReq req) {
        userService.wearingBadge(RequestHolder.get().getUid(), req);
        return ApiResult.success();
    }

    /**
     * 拉黑用户
     */
    @PutMapping("black")
    public ApiResult<Void> black(@Validated @RequestBody BlackReq req) {
        Long uid = RequestHolder.get().getUid();
        //先判断这个用户是否有权限 只有管理员能拉黑用户
        boolean hasPower = roleService.hasPower(uid, RoleEnum.ADMIN);
        AssertUtil.isTrue(hasPower, "您暂无拉黑权限噢");
        userService.blackUser(req);
        return ApiResult.success();
    }

    @PostMapping("/public/summary/userInfo/batch")
    @ApiOperation("用户聚合信息-返回的代表需要刷新的")
    public ApiResult<List<SummeryInfoVO>> getSummeryUserInfo(@Valid @RequestBody SummeryInfoReq req) {
        return ApiResult.success(userService.getSummeryUserInfo(req));
    }

    @PostMapping("/public/badges/batch")
    @ApiOperation("徽章聚合信息-返回的代表需要刷新的")
    public ApiResult<List<ItemInfoVO>> getItemInfo(@Valid @RequestBody ItemInfoReq req) {
        return ApiResult.success(userService.getItemInfo(req));
    }

}
